
package mas;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.*;

/**
 * Class creates and instantiates a simple graphical user interface for the 
 * multi-agent system 
 * 
 * Advanced Java Programming - ICA 2 Middleware 
 * @author Carl Morphet, Matt Constable, Stephen Pammenter
 * Teesside University
 * @version %I% 
 * Created: Apr 12, 2012
 */
public class GUI extends JPanel implements ActionListener, MouseListener
{
    /** The various components used in the GUI */
    private JPanel containerPanel, northPanel, middlePanel, splitPanel, southPanel;
    private JLabel createAgentLabel, sendMessageLabel;
    private JTextField agentTextField, fromTextField, toTextField, messageContents;
    public JTextArea monitorTextArea;
    private JButton createAgentBtn, sendMessageBtn;
    
    /** Constructor which instantiates the various GUI components */
    public GUI()
    {
        containerPanel = new JPanel(new GridLayout(2, 1));
        northPanel = new JPanel(new GridLayout(2, 3));
        middlePanel = new JPanel(new GridLayout(1, 2));
        splitPanel = new JPanel(new GridLayout(2, 1));
        southPanel = new JPanel();
        
        createAgentLabel = new JLabel("Create new Agent: ");
        sendMessageLabel = new JLabel("Send new message: ");
        
        agentTextField = new JTextField("Agent Name");
        fromTextField = new JTextField("From");
        toTextField = new JTextField("To");
        messageContents = new JTextField("Message content");
        monitorTextArea = new JTextArea(10, 5);
        
        createAgentBtn = new JButton("Create Agent");
        sendMessageBtn = new JButton("Send Message");

        northPanel.add(createAgentLabel);
        northPanel.add(agentTextField);
        northPanel.add(createAgentBtn);
        northPanel.add(sendMessageLabel);
        northPanel.add(middlePanel);
        northPanel.add(sendMessageBtn);
        northPanel.setBackground(Color.WHITE);
        
        middlePanel.add(splitPanel);
        middlePanel.add(messageContents);
        middlePanel.setBackground(Color.WHITE);
        
        splitPanel.add(fromTextField);
        splitPanel.add(toTextField);
        splitPanel.setBackground(Color.WHITE);
        
        southPanel.add(monitorTextArea);
        southPanel.setBackground(Color.WHITE);
        
        containerPanel.add(northPanel);
        containerPanel.add(southPanel);
        containerPanel.setBackground(Color.WHITE);
        
        this.add(containerPanel);
        this.setBackground(Color.WHITE);
        this.addListeners();
    }

    /** Method which add the relevant Action and Mouse listeners to the various
     * components 
     */
    private void addListeners()
    {
        createAgentBtn.addActionListener(this);
        sendMessageBtn.addActionListener(this);
        
        agentTextField.addMouseListener(this);
        fromTextField.addMouseListener(this);
        toTextField.addMouseListener(this);
        messageContents.addMouseListener(this);
    }
    
    /** Method which handles the button events appropriately 
     * 
     * @param e The action event thrown 
     */
    @Override
    public void actionPerformed(ActionEvent e)
    {
        //Action handling code here 
    }

    /** Method which handles mouse click actions appropriately 
     * 
     * @param e The mouse event thrown 
     */
    @Override
    public void mouseClicked(MouseEvent e) 
    {
        if(e.getSource() == agentTextField)
            agentTextField.setText("");
        
        else if(e.getSource() == fromTextField)
            fromTextField.setText("");
        
        else if(e.getSource() == toTextField)
            toTextField.setText("");
        
        else if(e.getSource() == messageContents)
            messageContents.setText("");
    }

    @Override
    public void mousePressed(MouseEvent e) {}

    @Override
    public void mouseReleased(MouseEvent e) {}

    @Override
    public void mouseEntered(MouseEvent e) {}

    @Override
    public void mouseExited(MouseEvent e) {}
}
